<template>
  <a-card :bordered="false">
    <a-row :gutter="[0, 20]">
      <a-col :span="18">
        <a-space>
          <a-input-search
            v-model="keyword"
            :placeholder="$t('all.autotext1716')"
            @search="onSearch"
          />
          <!--          <a-input-search v-model="contractCode" :placeholder="$t('all.autotext327')"  @search="onSearch" />-->
        </a-space>
        <a-select
          class="select_box"
          @change="classifyChange"
          v-model="$store.state.user.classify"
        >
          <a-select-option :value="1">正式</a-select-option>
          <a-select-option :value="2">测试</a-select-option>
        </a-select>
      </a-col>

      <a-col :span="6" align="right" v-if="$auth('BUSINESS.BUSINESS_ADD')">
        <a-button type="primary" @click="handleAdd()">{{
          $t('all.autotext1717')
        }}</a-button>
      </a-col>
    </a-row>
    <s-table
      ref="table"
      size="default"
      :row-key="(record) => record.id"
      :scroll="{ x: 'max-content' }"
      :pageSize="5"
      :pagination="{
        pageSizeOptions: ['5', '10', '20', '30']
      }"
      :data="loadData"
    >
      <a-table-column
        fixed="left"
        :title="$t('all.autotext334')"
        key="id"
        data-index="id"
      >
        <template slot-scope="text, row, index">{{
          index | getPageIndex($refs.table)
        }}</template>
      </a-table-column>
      <a-table-column
        fixed="left"
        :title="$t('all.autotext1718')"
        key="businessCode"
        data-index="businessCode"
      >
        <template slot-scope="text, record">
          <a v-if="$auth('ENTRY.ENTRY_LIST')" @click="handleDetail(record)">{{
            text
          }}</a>
          <span v-else>{{ text }}</span>
        </template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1719')"
        key="sponsorName"
        data-index="sponsorName"
      >
        <template slot-scope="text">{{ text }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1789')"
        key="customerName"
        data-index="customerName"
      >
        <template slot-scope="text">{{ text }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1829')"
        key="advancedPay"
        data-index="advancedPay"
      >
        <template slot-scope="advancedPay">{{
          advancedPay === 1 ? $t('all.autotext279') : $t('all.autotext957')
        }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1832')"
        key="cashFlowType"
        data-index="cashFlowType"
      >
        <template slot-scope="cashFlowType">{{
          cashFlowType === 1 ? $t('all.autotext1834') : $t('all.autotext1833')
        }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1790')"
        key="contractAmount"
        data-index="contractAmount"
      >
        <template slot-scope="text">{{ text }}</template>
      </a-table-column>
      <a-table-column
        :title="`${$t('all.autotext1720')}(${$t('all.autotext811')})`"
        key="totalCashFlow"
        data-index="totalCashFlow"
      >
        <template slot-scope="text">{{ text?.toFixed(2) }}</template>
      </a-table-column>
      <a-table-column
        :title="`${$t('all.autotext1016')}(${$t('all.autotext811')})`"
        key="paidAmount"
        data-index="paidAmount"
      >
        <template slot-scope="text">{{ text?.toFixed(2) }}</template>
      </a-table-column>
      <a-table-column
        v-if="$store.state.user.classify === 1"
        :title="$t('all.autotext1721')"
        key="warning"
        data-index="warning"
      >
        <template slot-scope="text">{{ text?.toFixed(2) }}</template>
      </a-table-column>
      <a-table-column
        v-else
        :title="$t('all.autotext1721')"
        key="uatWarning"
        data-index="uatWarning"
      >
        <template slot-scope="text">{{ text?.toFixed(2) }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1768')"
        key="creater"
        data-index="creater"
      >
        <template slot-scope="text">{{ text }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext666')"
        key="createTime"
        data-index="createTime"
      >
        <template slot-scope="text">{{ text | moment() }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1769')"
        key="updateTime"
        data-index="updateTime"
      >
        <template slot-scope="text">{{
          text ? $options.filters.moment(text, 'YYYY-MM-DD HH:mm:ss') : '-'
        }}</template>
      </a-table-column>
      <a-table-column
        :title="$t('all.autotext1458')"
        key="operator"
        data-index="operator"
      >
        <template slot-scope="text">{{ text }}</template>
      </a-table-column>
      <a-table-column :title="$t('all.autotext187')" width="280px">
        <template slot-scope="record">
          <a-space size="large">
            <a
              :disabled="record.status === 1"
              v-if="$auth('BUSINESS.BUSINESS_EDIT')"
              @click="handleAdd(record)"
              >{{ $t('all.autotext919') }}</a
            >
            <a
              :disabled="record.status === 1"
              v-if="
                $auth('BUSINESS.BUSINESS_DELETE') &&
                $store.state.user.classify !== 2
              "
              @click="handleDel(record)"
              >{{ $t('all.autotext379') }}</a
            >
            <a
              v-if="$auth('BUSINESS.BUSINESS_EXPORT')"
              @click="handleExport(record)"
              >{{ $t('all.autotext1793') }}</a
            >
            <a-popconfirm
              v-if="record.status !== 1 && $store.state.user.classify !== 2"
              :title="$t('是否关闭当前项⽬') + '？'"
              @confirm="handleClose(record)"
            >
              <a v-if="$auth('BUSINESS.BUSINESS_CLOSE')">{{
                $t('all.autotext892')
              }}</a>
            </a-popconfirm>
          </a-space>
        </template>
      </a-table-column>
    </s-table>
    <businessDrawer
      :status="status"
      :cashFlowType="cashFlowType"
      ref="businessRef"
      @refresh="$refs.table.refresh(true)"
    />
    <addEntry ref="addRef" @refresh="$refs.table.refresh(true)" />
    <warnDetail ref="warnRef" />
    <exportModal ref="exportRef" />
    <closeModal
      :visible="closeVisible"
      :dataList="closeDataList"
      @handleClose="closeVisible = false"
      ref="AuditFailure"
    />
    <!-- <identityCheck ref="identityRef" @ok="handleOk" /> -->
  </a-card>
</template>

<script>
import {
  busNumberList,
  getListByBusinessCode,
  busNumberDel,
  busNumberClose
} from '@/api/entry/entry'
import addEntry from './components/addEntry'
import businessDrawer from './components/businessDrawer'
import warnDetail from './components/warnDetail'
import exportModal from './components/exportModal'
import closeModal from './components/closeModal'
// import identityCheck from '@views/cost-auditing/components/identityCheck'
export default {
  components: { exportModal, addEntry, businessDrawer, warnDetail, closeModal },
  data() {
    return {
      keyword: '',
      closeVisible: false,
      contractCode: undefined,
      status: 0,
      cashFlowType: 0,
      loadData: (parameter) => {
        return busNumberList({
          businessCode: this.keyword,
          contractCode: this.contractCode,
          classify: this.$store.state.user.classify,
          ...parameter
        }).then((res) => {
          return res
        })
      },
      closeDataList: []
    }
  },
  methods: {
    onSearch(e) {
      this.$refs.table.refresh(true)
    },
    classifyChange(value) {
      this.$store.commit('SET_CLASSIFY', value)
      this.$refs.table.refresh(true)
      this.$refs.businessRef.againOpen()
    },
    handleClose(record) {
      busNumberClose({ id: record.id }).then((res) => {
        // console.log(res)
        if (res.type === 1) {
          this.$message.success('操作成功！')
          this.$refs.table.refresh(true)
        } else if (res.type === 2) {
          this.closeDataList = res.list
          this.closeVisible = true
        } else if (res.type === 3) {
          this.$message.warning(
            '合同存在未完成⼊账的开票记录，请完成⼊账后即可关闭！'
          )
        } else if (res.type === 4) {
          this.$message.warning('合同下存在未完成的申请单，请完成申请单后即可关闭')
        }
      })
    },
    handleAdd(record) {
      this.$refs.addRef.open(record)
    },
    handleDetail(record) {
      if (this.$auth('ENTRY.ENTRY_LIST')) {
        this.status = record.status
        this.cashFlowType = record.cashFlowType || 0
        this.$refs.businessRef.open(record)
      }
    },
    async handleDel(record) {
      const res = await getListByBusinessCode({
        businessCode: record.businessCode,
        classify: this.$store.state.user.classify
      })
      if (res.length) {
        this.$refs.warnRef.open(res, 1)
      } else {
        const that = this
        this.$confirm({
          title: this.$t('all.autotext1059'),
          okType: 'danger',
          onOk() {
            that.sureDel(record)
          }
        })
      }
    },
    async sureDel(record) {
      await busNumberDel({ id: record.id })
      this.$refs.table.refresh(true)
    },
    // 导出
    async handleExport(record) {
      this.$refs.exportRef.open(2, record.businessCode)
    }
  }
}
</script>

<style scoped>
.select_box {
  width: 120px;
  margin-left: 30px;
}
</style>
